package com.sweets.look.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.sweets.look.R;
import com.sweets.look.ui.widget.MarkProgressDialog;

public class UploadUtils {
    public interface OnUploadListener {
        void failed();

        void success();
    }

    private static final int STATUS_UPLOAD = 111;
    private Activity mActivity;
    private Context mContext;
    @SuppressLint(value = {"HandlerLeak"})
    private Handler mHandler;
    private OnUploadListener mListener;
    private MarkProgressDialog mWaitDialog;

    public UploadUtils(Context context) {
        super();
        this.mHandler = new Handler() {
            public void handleMessage(Message msg) {

                super.handleMessage(msg);
                UploadUtils.this.mWaitDialog.dismiss();
                switch (msg.what) {
                    case STATUS_UPLOAD: {
                        if (((String) msg.obj).equalsIgnoreCase("0")) {
                            if (UploadUtils.this.mListener != null) {
                                UploadUtils.this.mListener.success();
                            } else {
                                ViewUtils.createInformativeDialog(UploadUtils.this.mActivity, UploadUtils
                                        .this.mContext.getResources().getString(R.string.dialog_message), UploadUtils.this
                                        .mContext.getResources().getString(R.string.upload_completed));
                            }
                        } else if (UploadUtils.this.mListener != null) {
                            UploadUtils.this.mListener.failed();
                        } else {
                            ViewUtils.createInformativeDialog(UploadUtils.this.mActivity, UploadUtils
                                    .this.mContext.getResources().getString(R.string.dialog_message), UploadUtils.this.mContext
                                    .getResources().getString(R.string.code_500));
                        }

                        break;
                    }
                }
            }
        };
        this.mContext = context;
        this.mActivity = ((Activity) context);
        this.getReady();
    }


    private void getReady() {
        this.mWaitDialog = new MarkProgressDialog(this.mContext);
        this.mWaitDialog.builder();
        this.mWaitDialog.setMessage(this.mContext.getString(R.string.toast_wait_a_minute));
        this.mWaitDialog.setCancelable(false);
    }

    private static StringBuffer getRequestData(Map<String, String> arg6) {
        StringBuffer v2 = new StringBuffer();
        try {
            Iterator<Entry<String, String>> v4 = arg6.entrySet().iterator();
            while (v4.hasNext()) {
                Entry<String, String> v1 = v4.next();
                v2.append(v1.getKey()).append("=").append(v1.getValue()).append(
                        "&");
            }

            v2.deleteCharAt(v2.length() - 1);
        } catch (Exception v0) {
            v0.printStackTrace();
        }

        return v2;
    }

    public void setOnUploadListener(OnUploadListener listener) {
        this.mListener = listener;
    }

    public void uploadInfo(final String urlString, final Map<String, String> params) {
        this.mWaitDialog.show();
        new Thread(new Runnable() {
            public void run() {
                byte[] req = UploadUtils.getRequestData(params).toString().getBytes();
                try {
                    HttpURLConnection connection = (HttpURLConnection) new URL(urlString).openConnection();
                    connection.setConnectTimeout(3000);
                    connection.setDoInput(true);
                    connection.setDoOutput(true);
                    connection.setRequestMethod("POST");
                    connection.setUseCaches(false);
                    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    connection.setRequestProperty("Content-Length", String.valueOf(req.length));
                    connection.getOutputStream().write(req);
                    int responseCode = connection.getResponseCode();
                    Message message = new Message();
                    message.what = STATUS_UPLOAD;
                    message.obj = responseCode == 200 ? "0" : "1";
                    UploadUtils.this.mHandler.sendMessage(message);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

